import 'package:flutter/material.dart';
import 'package:flutter_xj_video/custom_drawer/drawer_item.dart';

import 'drawer_resource_controller.dart';

class DrawerResource extends StatefulWidget {
  final AnimationController iconAnimationController;
  final DrawerIndex screenIndex;
  final Function(DrawerItem) callBackItem;

  const DrawerResource(
      {Key key,
      this.iconAnimationController,
      this.screenIndex,
      this.callBackItem})
      : super(key: key);

  @override
  _DrawerResourceState createState() => _DrawerResourceState();
}

class _DrawerResourceState extends State<DrawerResource> {
  List<DrawerItem> drawerList;

  @override
  void initState() {
    setDrawerListArray();
    super.initState();
  }

  void setDrawerListArray() {
    drawerList = <DrawerItem>[
      DrawerItem(
        index: DrawerIndex.Mahuazy,
        labelName: '麻花资源',
        api: 'https://www.mhapi123.com/inc/api.php',
        icon: Icon(Icons.ondemand_video),
      ),
      DrawerItem(
        index: DrawerIndex.Zyw88,
        labelName: '88 影视资源站',
        api: 'http://www.88zyw.net/inc/api.php',
        isAssetsImage: true,
        icon: Icon(Icons.ondemand_video),
      ),
      DrawerItem(
        index: DrawerIndex.Apibdzy,
        labelName: '百度云资源',
        api: 'https://api.apibdzy.com/api.php/provide/vod/at/xml',
        icon: Icon(Icons.ondemand_video),
      ),
      // DrawerItem(
      //   index: DrawerIndex.Mbo,
      //   api: 'http://caiji.mb77.vip/inc/api.php',
      //   labelName: '秒播资源',
      //   icon: Icon(Icons.help),
      // ),
      // DrawerItem(
      //   index: DrawerIndex.Zuidazy,
      //   api: 'http://www.zdziyuan.com/inc/api.php',
      //   labelName: '最大资源网',
      //   icon: Icon(Icons.help),
      // ),
      DrawerItem(
        index: DrawerIndex.Ku123,
        labelName: '123 资源',
        api: 'http://cj.123ku2.com:12315/inc/api.php',
        icon: Icon(Icons.ondemand_video),
      ),
      // DrawerItem(
      //   index: DrawerIndex.Okzy,
      //   labelName: 'OK 资源网',
      //   api: 'http://cj.okzy.tv/inc/api.php',
      //   icon: Icon(Icons.help),
      // ),
      // DrawerItem(
      //   index: DrawerIndex.Kuyunzy,
      //   labelName: '酷云资源',
      //   api: 'http://caiji.kuyun98.com/inc/ldg_api.php',
      //   icon: Icon(Icons.help),
      // ),
      // DrawerItem(
      //   index: DrawerIndex.Kubozy,
      //   api: 'http://api.kbzyapi.com/inc/api.php',
      //   labelName: '酷播资源',
      //   icon: Icon(Icons.help),
      // ),
      // DrawerItem(
      //   index: DrawerIndex.Yongjiuzy,
      //   api: 'http://cj.yongjiuzyw.com/inc/api.php',
      //   labelName: '永久资源',
      //   icon: Icon(Icons.help),
      // ),
      DrawerItem(
        index: DrawerIndex.Rrzy,
        api: 'https://www.rrzyw.cc/api.php/provide/vod/from/rrm3u8/at/xml/',
        labelName: '人人资源',
        icon: Icon(Icons.ondemand_video),
      ),
      // DrawerItem(
      //   index: DrawerIndex.Bbkdj,
      //   labelName: '步步高顶尖资源网',
      //   api: 'http://api.bbkdj.com/api',
      //   icon: Icon(Icons.help),
      // ),
      // DrawerItem(
      //   index: DrawerIndex.Zuixinzy,
      //   labelName: '最新资源',
      //   api: 'http://api.zuixinapi.com/inc/api.php',
      //   icon: Icon(Icons.help),
      // ),
      DrawerItem(
        index: DrawerIndex.Subo988,
        labelName: '速播资源站',
        api: 'https://www.subo988.com/inc/api.php',
        icon: Icon(Icons.ondemand_video),
      ),
      DrawerItem(
        index: DrawerIndex.B1886zy,
        api: 'http://cj.1886zy.co/inc/api.php',
        labelName: '1886 资源',
        icon: Icon(Icons.ondemand_video),
      ),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          SizedBox(
            height: 60,
          ),
          Expanded(
            child: ListView.builder(
              physics: const BouncingScrollPhysics(),
              padding: const EdgeInsets.all(0.0),
              itemCount: drawerList.length,
              itemBuilder: (BuildContext context, int index) {
                return inkwell(drawerList[index]);
              },
            ),
          )
        ],
      ),
    );
  }

  Widget inkwell(DrawerItem listData) {
    return InkWell(
        splashColor: Colors.grey.withOpacity(0.1),
        highlightColor: Colors.red,
        onTap: () {
          navigationtoScreen(listData.index);
        },
        child: Stack(
          children: [
            Container(
              height: 46,
              child: Row(
                children: [
                  SizedBox(
                    width: 10,
                    height: 46,
                  ),
                  Icon(listData.icon.icon,
                      color: widget.screenIndex == listData.index
                          ? Colors.blue
                          : Colors.black),
                  const Padding(
                    padding: EdgeInsets.all(4.0),
                  ),
                  Text(
                    listData.labelName,
                    style: TextStyle(
                      fontWeight: FontWeight.w500,
                      fontSize: 20,
                      color: widget.screenIndex == listData.index
                          ? Colors.blue
                          : Colors.black,
                    ),
                    textAlign: TextAlign.left,
                  ),
                ],
              ),
            ),
            widget.screenIndex == listData.index
                ? AnimatedBuilder(
                    animation: widget.iconAnimationController,
                    builder: (BuildContext context, Widget child) {
                      return Transform(
                        transform: Matrix4.translationValues(
                            (MediaQuery.of(context).size.width * 0.75 - 64) *
                                (1.0 -
                                    widget.iconAnimationController.value -
                                    1.0),
                            0.0,
                            0.0),
                        child: Padding(
                          padding: EdgeInsets.only(top: 4, bottom: 4),
                          child: Container(
                            width:
                                MediaQuery.of(context).size.width * 0.75 - 64,
                            height: 38,
                            decoration: BoxDecoration(
                              color: Colors.blue.withOpacity(0.2),
                              borderRadius: new BorderRadius.only(
                                topLeft: Radius.circular(0),
                                topRight: Radius.circular(28),
                                bottomLeft: Radius.circular(0),
                                bottomRight: Radius.circular(28),
                              ),
                            ),
                          ),
                        ),
                      );
                    },
                  )
                : const SizedBox(
                    height: 46,
                  )
          ],
        ));
  }

  Future<void> navigationtoScreen(DrawerIndex indexScreen) async {
    widget.callBackItem(drawerList[indexScreen.index]);
  }
}
